package com.victualler.core.common.web;
 

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController;
  
/**
 * 请求session对象中的值
 * @author Guoshichang
 */
@RestController
@RequestMapping("/session")
public class SessionController {
	/**
	 * 根据key获取对应的对象
	 * @param key 
	 * @param session
	 * @return
	 */
	@RequestMapping("/get/{key}")
	public Object get(@PathVariable("key")String key,HttpSession session){
		Object ob = session.getAttribute(key);
		 return  ob;
	}
	/**
	 * 根据key删除对应的对象
	 * @param key
	 * @param session
	 * @return
	 */
	@RequestMapping("/remove/{key}")
	public boolean remove(@PathVariable("key")String key,HttpSession session){
		try{
			 session.removeAttribute(key);
			 return true;
		}catch(Exception e){ 
			return false;
		} 
	}
	
	/**
	 * 根据key获取对应的对象
	 * @param key 
	 * @param session
	 * @return
	 */
	@RequestMapping("/set/{key}")
	public boolean get(@PathVariable("key")String key,@RequestBody Map<String,String> objectMap,HttpSession session){
		try{
			 session.setAttribute(key, objectMap);
			 return true;
		}catch(Exception e){ 
			return false;
		} 
	}
}
